package cn.zhoutingrui.pethome.system.shop.service.impl;

import cn.zhoutingrui.pethome.base.constants.BaseConstants;
import cn.zhoutingrui.pethome.system.emp.domain.Employee;
import cn.zhoutingrui.pethome.system.emp.mapper.EmployeeMapper;
import cn.zhoutingrui.pethome.system.shop.domain.Shop;
import cn.zhoutingrui.pethome.system.shop.domain.ShopAuditLog;
import cn.zhoutingrui.pethome.system.shop.mapper.ShopAuditLogMapper;
import cn.zhoutingrui.pethome.system.shop.mapper.ShopMapper;
import cn.zhoutingrui.pethome.system.shop.service.IShopAuditLogService;
import cn.zhoutingrui.pethome.base.service.impl.BaseServiceImpl;
import cn.zhoutingrui.pethome.utils.SendEmail;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author ztr
 * @since 2023-01-25
 */
@Service
public class ShopAuditLogServiceImpl extends BaseServiceImpl<ShopAuditLog> implements IShopAuditLogService {
    @Autowired
    private ShopMapper shopMapper;
    @Autowired
    private ShopAuditLogMapper shopAuditLogMapper;
    @Autowired
    private EmployeeMapper employeeMapper;
    @Autowired
    private SendEmail sendEmail;
    @Override
    public void add(ShopAuditLog shopAuditLog){
        try {
            //修改状态为待激活
            shopAuditLog.setAuditId(334l);
            shopAuditLog.setAuditTime(new Date());
            Long shopId = shopAuditLog.getShopId();
            //获取店铺数据
            Shop byId = shopMapper.findById(shopId);
            //修改店铺状态
            byId.setState(shopAuditLog.getState());

            //添加审核日志
            shopAuditLogMapper.add(shopAuditLog);
            //获取店主信息
            Employee emp = employeeMapper.findById(byId.getAdminId());
            //获取邮箱
            String email = emp.getEmail();
            if (shopAuditLog.getState()==4){
                //驳回
                emp.setState(BaseConstants.Employee.STATE_LOCK);
                String text="<h1>您的账号审核未通过</h1>" +"<div>原因为"+shopAuditLog.getNote()+"</div>";
                sendEmail.send2("1239703611@qq.com","你的账号未通过审核",text,true,"w1239703611@163.com");
            }else {
                //审核通过
                //修改店主状态
                emp.setState(BaseConstants.Employee.STATE_WAIT_ACTIVE);
                //获取发送信息
                String text="<h1>点击以下链接进行激活,如果不是本人账号请忽略<h1><a href=http://localhost:8080/shop/active/"+shopId+">激活</a>";
                //发送邮件
                sendEmail.send2("1239703611@qq.com","宠物之家店铺激活",text,true,"w1239703611@163.com");
            }
            shopMapper.update(byId);
            //保存修改后的店长信息
            employeeMapper.update(emp);

        } catch (Exception e) {
            throw new RuntimeException(e);
        }

    }
}
